package com.qzsoft.zhjd.server.modules.cement.controller;

import com.qzsoft.zhjd.server.modules.cement.service.CollectCementDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.cement.model.CementCollectDataModel;
import com.qzsoft.zhjd.server.socket.config.TcpReceiveConstants;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName CementReceiveController
 * @Description 水稳数据接收
 * @author linhuawei
 * @since 2022-01-18
 */
@Slf4j
@RestController
@RequestMapping("/datacollect/cement")
public class CollectCementReceiveController {

    @Autowired
    private CollectCementDataService collectCementDataService;

    @PostMapping("/receive")
    public R receive(@RequestBody String msg) {
        try {
            CementCollectDataModel originalData = JSONObject.parseObject(msg, CementCollectDataModel.class);
            if (TcpReceiveConstants.TYPE_CEMENT_DATA.equals(originalData.getType())) {
                log.info("水稳拌合站数据：{}", originalData);
                collectCementDataService.save(originalData);
                return R.ok(originalData.getId());
            }
            log.error("水稳拌合站数据类型错误：{}", msg);
        } catch (Exception e) {
            log.error("水稳拌合站数据处理失败：{}", msg);
        }
        return R.error();
    }

}
